package com.study.hot.jetpack.navigation.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.study.hot.R
import com.study.hot.databinding.FragmentGoodsDetailBinding

import com.study.hot.jetpack.navigation.GoodsViewModel
import kotlinx.android.synthetic.main.fragment_goods_detail.*

/**
 * A simple [Fragment] subclass.
 */
class GoodsDetailFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        val goodsViewModel = ViewModelProvider(requireActivity())[GoodsViewModel::class.java]
        val binding = DataBindingUtil.inflate<FragmentGoodsDetailBinding>(inflater, R.layout.fragment_goods_detail, container, false)
        binding.data = goodsViewModel
        binding.lifecycleOwner = activity
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //使用使用createNavigateOnClickListener创建按钮的点击事件
        goListBtn.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_goodsDetailFragment_to_goodsListFragment))

        //获取参数
        arguments?.apply {
            val id = getString("id")
            val name = getString("name")
            textView.text = "$id :: $name"
        }
    }
}
